package com.example.demo.RecursionTest;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.RecursionTest.entity.Jurisdiction;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class DecursionMainTest {

    public static void main(String[] args) {
        List<Jurisdiction> list = new ArrayList<>();

        Jurisdiction j1 = new Jurisdiction();
        j1.setPowerId(1);
        j1.setName("一级权限1");
        j1.setXuanzhongId(0);
        j1.setDengji(1);
        j1.setBianma("001");
        j1.setShangjibianma("000");

        Jurisdiction j2 = new Jurisdiction();
        j2.setPowerId(2);
        j2.setName("一级权限2");
        j2.setXuanzhongId(0);
        j2.setDengji(1);
        j2.setBianma("002");
        j2.setShangjibianma("000");

        Jurisdiction j3 = new Jurisdiction();
        j3.setPowerId(3);
        j3.setName("一级权限3");
        j3.setXuanzhongId(0);
        j3.setDengji(1);
        j3.setBianma("003");
        j3.setShangjibianma("000");

        Jurisdiction j4 = new Jurisdiction();
        j4.setPowerId(4);
        j4.setName("二级权限1");
        j4.setXuanzhongId(0);
        j4.setDengji(2);
        j4.setBianma("001001");
        j4.setShangjibianma("001");

        Jurisdiction j5 = new Jurisdiction();
        j5.setPowerId(5);
        j5.setName("二级权限2");
        j5.setXuanzhongId(0);
        j5.setDengji(2);
        j5.setBianma("001002");
        j5.setShangjibianma("001");

        Jurisdiction j7 = new Jurisdiction();
        j7.setPowerId(7);
        j7.setName("三级权限1");
        j7.setXuanzhongId(0);
        j7.setDengji(3);
        j7.setBianma("001002001");
        j7.setShangjibianma("001002");

        Jurisdiction j8 = new Jurisdiction();
        j8.setPowerId(8);
        j8.setName("三级权限2");
        j8.setXuanzhongId(0);
        j8.setDengji(2);
        j8.setBianma("001002002");
        j8.setShangjibianma("001002");

        list.add(j1);
        list.add(j2);
        list.add(j3);
        list.add(j4);
        list.add(j5);
        list.add(j7);
        list.add(j8);
        List<Jurisdiction> collect = list.stream().filter(x -> x.getDengji().equals(1)).collect(Collectors.toList());
        List<Jurisdiction> newOne = one(list, collect, 2);
        System.out.println(JSONObject.toJSONString(newOne));
    }

    private static List<Jurisdiction> one(List<Jurisdiction> lowData, List<Jurisdiction> newData, int i) {
        int finalI = i;
        List<Jurisdiction> collect = lowData.stream().filter(x -> x.getDengji().equals(finalI)).collect(Collectors.toList());
        if (collect.size() > 0) {
            for (Jurisdiction jurisdiction : newData) {
                List<Jurisdiction> jurisdictions = collect.stream().filter(x -> x.getShangjibianma().equals(jurisdiction.getBianma())).collect(Collectors.toList());
                jurisdiction.setJurisdictionList(one(lowData, jurisdictions, i++));
            }
        }

        return newData;
    }

}
